package com.zhuiyun.educations.api.address.service.impl;

import com.zhuiyun.educations.api.address.entity.AddressEntity;
import com.zhuiyun.educations.api.address.mapper.AddressMapper;
import com.zhuiyun.educations.api.address.service.AddressService;
import com.zhuiyun.educations.common.response.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

/**
 * @ClassName AddressServiceImpl
 * @Description TODO
 * @Author cjl
 * @Date 2023/6/23 11:38
 **/
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    AddressMapper addressMapper;
    /**
    * @Author cjl
    * @Description 通过用户id查询用户收货地址
    * @Date 2023/6/23 14:22
    * @Return com.zhuiyun.educations.common.response.CommonResult
    * @param id
    */
    @Override
    public CommonResult selectAddressById(Integer id) {
        Example example = new Example(AddressEntity.class);
        example.createCriteria().andEqualTo("userId",id);
        AddressEntity addressEntity = addressMapper.selectOneByExample(example);
        return CommonResult.ok(addressEntity);
    }
    /**
    * @Author cjl
    * @Description 修改用户绑定的地址
    * @Date 2023/6/26 8:54
    * @Return com.zhuiyun.educations.common.response.CommonResult
    * @param addressEntity
    */
    @Override
    public CommonResult updateAddress(AddressEntity addressEntity) {
        addressEntity.setUpdateTime(new Date());
        int i = addressMapper.updateByPrimaryKeySelective(addressEntity);
        if (i!=0){
            return CommonResult.ok("恭喜你，修改成功！");
        }else {
            return CommonResult.error(1000,"修改失败!!!");
        }
    }
    /**
     * @Author cjl
     * @Description 绑定用户地址
     * @Date 2023/6/26 8:54
     * @Return com.zhuiyun.educations.common.response.CommonResult
     * @param addressEntity
     */
    @Override
    public CommonResult addAddress(AddressEntity addressEntity) {
        addressEntity.setCreateTime(new Date());
        addressEntity.setUpdateTime(new Date());
        int i = addressMapper.insertSelective(addressEntity);
        if (i!=0){
            return CommonResult.ok("恭喜你，添加成功！");
        }else {
            return CommonResult.error(1000,"修改失败!!!");
        }
    }


}
